unit nomenclaturecreateeditgroup;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DbCtrls,
  StdCtrls, Buttons;

type

  { TfrmNomenclatureCreateEditGroup }

  TfrmNomenclatureCreateEditGroup = class(TForm)
    btnGroupSelect: TBitBtn;
    btnSave: TBitBtn;
    btnCencel: TBitBtn;
    edtGroupTitle: TEdit;
    edtGroup: TEdit;
    lblTitle: TLabel;
    lblGroup: TLabel;
    procedure btnCencelClick(Sender: TObject);
    procedure btnGroupSelectClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure checkErrors;
    procedure DBEdtGroupTitleChange(Sender: TObject);
    procedure edtGroupTitleChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    isNew:Boolean;
    isEdit:Boolean;
  end;

var
  frmNomenclatureCreateEditGroup: TfrmNomenclatureCreateEditGroup;

implementation

{$R *.lfm}
uses main,nomenclature,groupselect;

{ TfrmNomenclatureCreateEditGroup }

procedure TfrmNomenclatureCreateEditGroup.checkErrors;
begin
  if (edtGroupTitle.Text=EmptyStr) then begin
    btnSave.Enabled:=False;
  end else begin
    btnSave.Enabled:=True;
  end;
end;

procedure TfrmNomenclatureCreateEditGroup.DBEdtGroupTitleChange(Sender: TObject
  );
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEditGroup.edtGroupTitleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEditGroup.FormCloseQuery(Sender: TObject;
  var CanClose: boolean);
begin
  isNew:=False;
  isEdit:=False;
  FrmMain.DSNomenclature.DataSet.Refresh;
end;

procedure TfrmNomenclatureCreateEditGroup.FormShow(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEditGroup.btnSaveClick(Sender: TObject);
begin
    if (edtGroup.Text=EmptyStr) then begin
    //   frmNomenclature.CurrentGroupRecordNumber:=0;
    end;
    if (isEdit) then begin
      FrmMain.ZQuery.SQL.Text:='UPDATE nomenclature SET title='''+edtGroupTitle.Text+''' WHERE id='+IntToStr(frmNomenclature.CurrentElementRecordNumber);
    end;
    if (isNew) then begin
      FrmMain.ZQuery.SQL.Text:='INSERT INTO nomenclature VALUES(NULL,1,'+IntToStr(frmNomenclature.CurrentGroupRecordNumber)+','''+edtGroupTitle.Text+''',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)';
    end;
    FrmMain.ZQuery.ExecSQL;
    FrmMain.DSNomenclature.DataSet.Refresh;
    isNew:=False;
    isEdit:=False;
    Close;
end;

procedure TfrmNomenclatureCreateEditGroup.btnCencelClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmNomenclatureCreateEditGroup.btnGroupSelectClick(Sender: TObject);
begin
  frmGroupSelect.groupsId:=1;
  frmGroupSelect.ShowModal;
  if frmGroupSelect.selectedGroupId<>EmptyStr then begin
    frmNomenclature.CurrentGroupRecordNumber:=StrToInt(frmGroupSelect.selectedGroupId);
    edtGroup.Text:=frmGroupSelect.selectedGroupTitle;
  end;
end;

end.

